/*******************************************************************************
** File Name:		Employee.js
** Creator:		Tiya.T.Tang
** Create date:	2010-10-15	
** Latest Modifier:	Tiya.T.Tang
** Latest Modify date:	
** Description:		与选人控件相关的js方法
** Modify History:
**		Modify Date		Modify User		Modify Description
**		 修改日期			  修改人				修改描述
**Code Review:
**		CodeReview Date	CodeReview User	CodeReview Scope
**		CodeReview日期	 CodeReview人		CodeReview的范围
** Version number:   1.0.0
*********************************************************************************/
//JS的String.fromat函数
String.format = function() {
    if (arguments.length == 0)
        return null;

    var str = arguments[0];
    for (var i = 1; i < arguments.length; i++) {
        var re = new RegExp('\\{' + (i - 1) + '\\}', 'gm');
        str = str.replace(re, arguments[i]);
    }
    return str;
}


//SearchEmployee
var SearchEmployeePara = null;
function SearchEmployee(popPageUrl, returnFunction, title, otherJsonParams) {
  
    SearchEmployeePara = {
        SearchEmployeeTitle: title,
        SearchEmployeeReturnFunction: returnFunction,
        SearchEmployeeOtherJsonParams: otherJsonParams
    }
    jQuery.facebox({ ajax: popPageUrl });
}

//添加用户
function AddEmployee(popPageUrl, keyName, teamID) {
    //alert(teamID);
    var returnFunction = 'EmployeeAdded("' + keyName + '",{0})';
    var jasonParams = "{\"KeyName\":\"" + keyName + "\","
                                 + ((teamID.length == 0 )? "" : ("\"TeamID\":\""+teamID+"\""))
                                  + "}";
    //alert(jasonParams);
    SearchEmployee(popPageUrl, returnFunction,"选择项目成员",jasonParams);
}

//处理添加指定的用户
function EmployeeAdded(keyName, returnEmployeeLists) {
    
    eval("UserArr_" + keyName + "=returnEmployeeLists;");
    RefreshUserList(keyName);
}

//更新用户列表
function RefreshUserList(keyName) {
    var ulCtrl = $("#ulUserList_" + keyName);
    var objUserArr;
    
    eval("objUserArr=UserArr_" + keyName);
    var html = "";
    if (ulCtrl) {
        for (var i = 0; i < objUserArr.length; i++) {
            html += "<li><span class='name'>" + objUserArr[i].FullName + "</span>" +
            "<a class='del' onclick=\"RemoveUser('" + keyName + "'," + i  + ");\"></a></li>";
        }
        ulCtrl.html(html);
        MyFun.grid(".addUserList li"); 
    }
}

//删除用户
function RemoveUser(keyName, i) {
    var objUserArrName = "UserArr_" + keyName;
    eval(objUserArrName + ".splice(i, 1)");

    RefreshUserList(keyName);
}

/**

* 从对象数组中获取属性为objPropery，值为objValue元素的对象

* @param Array arrPerson 数组对象

* @param String objPropery 对象的属性

* @param String objPropery 对象的值;

* @return Array 过滤后的数组

*/

function GetObjFromArr(arr, objPropery, objValue) {

    return $.grep(arr, function(cur, i) {

        return cur[objPropery] == objValue;

    });

}

function GetObjFromArrCompareStrPropery(arr, strPropery, strValue,ignoreCase) {

    return $.grep(arr, function(cur, i) {

        if (ignoreCase) {
            return cur[strPropery].toLowerCase() == strValue.toLowerCase();
        }
        else {
            return cur[strPropery] == strValue;
        }

    });

}

String.prototype.trim = function() {
    return
    (this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""));
}
String.prototype.startsWith = function(str) {
    return (this.match("^" + str) == str);
}
String.prototype.endsWith = function(str) {
    return (this.match(str + "$") == str); 
}
